package fr.n7.khome.core;

import java.util.HashMap;

import fr.n7.khome.model.Lodging;
import fr.n7.khome.model.LodgingFieldName;

/**
 * It contains the likelihood and the necessity between
 * each fields of a request and a lodging.
 * It also contains the global likelihood and necessity.
 * 
 * @see fr.n7.khome.core.Request
 * @author bbaccot && clej37
 *
 */
public class Result implements Comparable<Result> {

	/**
	 * The likelihood between each fields of a pattern (request) and
	 * a lodging.
	 */
	private HashMap<LodgingFieldName,Float> likelihood;
	
	/**
	 * The necessity between each fields of a pattern (request) and
	 * a lodging.
	 */
	private HashMap<LodgingFieldName,Float> necessity;
	
	/**
	 * The global likelihood
	 */
	private float glikelihood;
	
	/**
	 * The likelihood sum
	 */
	private float sumlikelihood;
	
	/**
	 * The global necessity
	 */
	private float gnecessity;
	
	/**
	 * The necessity sum
	 */
	private float sumnecessity;
	
	/**
	 * The lodging (likelihood/necessity / this lodging)
	 */
	private Lodging lodging;
	
	/**
	 * Make a result for a lodging
	 * 
	 * @param l - a lodging
	 */
	public Result(Lodging l) {
		likelihood = new HashMap<LodgingFieldName, Float>();
		necessity = new HashMap<LodgingFieldName, Float>();
		this.lodging = l;
	}

	/**
	 * To sort results.
	 * 
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 * x R y <=> x.compareTo(y)<=0
	 */
	public int compareTo(Result y) {
		//By necessity
		int r=-Float.compare(this.getGnecessity(), y.getGnecessity());
		// if necessity are equal...
		if(r==0) {
			// ... by likelihood
			r=-Float.compare(this.getGlikelihood(), y.getGlikelihood());
			// if likelihood are equal...
			if(r==0) {
				// ... by necessity sum ==> why not ! (it gives quite good results for colors in the view)
				r=-Float.compare(this.getSumnecessity(), y.getSumnecessity());
				// ...if necessity sum are equal...
				if(r==0) {
					// ... by likelihood sum
					r=-Float.compare(this.getSumlikelihood(), y.getSumlikelihood());
					if (r==0) {
						// ... by lodging id... 
						r = (int) (this.getLodging().getId() - y.getLodging().getId());
					}
				}
			}
		}
		return r;
	}
	
	public HashMap<LodgingFieldName, Float> getLikelihood() {
		return likelihood;
	}

	public HashMap<LodgingFieldName, Float> getNecessity() {
		return necessity;
	}
	
	public float getGlikelihood() {
		return glikelihood;
	}

	public void setGlikelihood(float glikelihood) {
		this.glikelihood = glikelihood;
	}

	public float getGnecessity() {
		return gnecessity;
	}
	
	public float getSumnecessity() {
		return sumnecessity;
	}

	public void setGnecessity(float gnecessity) {
		this.gnecessity = gnecessity;
	}
	
	public void setSumnecessity(float sumnecessity) {
		this.sumnecessity = sumnecessity;
	}

	public Lodging getLodging() {
		return lodging;
	}
	
	public String toString(){
		return lodging+"\n"+
					"GLi = "+glikelihood+", "+
					"Li = "+likelihood+", "+
					"GNe = "+gnecessity+", "+
					"Ne = "+necessity;
	}

	public float getSumlikelihood() {
		return this.sumlikelihood;
	}
	
	public void setSumlikelihood(float sumlikelihood) {
		this.sumlikelihood = sumlikelihood;
	}
	
}
